<template>
  <div class="user-container">
    <el-form ref="form" :inline="true" :model="form" label-width="40px">
        <el-form-item label="角色">
            <el-input v-model="form.roleName" placeholder="输入角色搜索" clearable>
                <el-button slot="append" icon="el-icon-search" @click="search"></el-button>
            </el-input>
        </el-form-item>  
    </el-form>
    <el-button icon="el-icon-plus" type="primary" @click="addCount">添加</el-button>
    <!-- <el-button icon="el-icon-edit" type="info" @click="editRole">修改</el-button>
    <el-button icon="el-icon-delete" type="warning" @click="delRole">删除</el-button> -->
    <div class="user-table">
        <el-table
        ref="multipleTable"
        :data="roleList"
        border
        stripe
        tooltip-effect="dark"
        style="width: 100%"
        @selection-change="handleSelectionChange">
        <el-table-column
        type="index"
        width="40">
        </el-table-column>
        <!-- <el-table-column
        type="selection"
        width="55">
        </el-table-column> -->
        <!-- <el-table-column
        prop="id"
        label="ID">   
        </el-table-column> -->
        <el-table-column
        prop="name"
        label="名称">   
        </el-table-column>
         <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="editRole(scope.$index, scope.row)">编辑</el-button>
          <el-button
            size="mini"
            type="danger"
            @click="delRole(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>

    <div class="user-pagination">
      <el-pagination
      background
      layout="prev, pager, next"
      :total="totalItem">
      </el-pagination>
    </div>

    <el-dialog ref='editrole' :title="dialogTitle" :rules="validateRule" :visible.sync="dialogFormVisible" center>
      <el-form :model="dialogForm" label-width="40px">
        <el-form-item label="名称">
          <el-input v-model="dialogForm.roleName" auto-complete="off" prop="name"></el-input>
        </el-form-item>
        <el-form-item label="权限">
          <el-input v-model="dialogForm.rolePermission" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="handleCancelSubmit">取 消</el-button>
        <el-button type="primary" @click="handleDialogSubmit('editrole')">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { mapGetters } from 'vuex'

export default {
  name: 'role',
  computed: {
    ...mapGetters([
      'name',
      'roles'
    ])
  },
  data() {
      return {
          form:{
              roleName:''
          },
          dialogForm:{
              roleName:'',
              rolePermission:''
          },
          roleList: [{
            date: '2016-05-03',
            name: '王小虎',
            address: '上海市普陀区金沙江路 1518 弄'
            }, {
            date: '2016-05-02',
            name: '王小虎',
            address: '上海市普陀区金沙江路 1518 弄'
            }, {
            date: '2016-05-04',
            name: '王小虎',
            address: '上海市普陀区金沙江路 1518 弄'
            }, {
            date: '2016-05-01',
            name: '王小虎',
            address: '上海市普陀区金沙江路 1518 弄'
            }, {
            date: '2016-05-08',
            name: '王小虎',
            address: '上海市普陀区金沙江路 1518 弄'
            }, {
            date: '2016-05-06',
            name: '王小虎',
            address: '上海市普陀区金沙江路 1518 弄'
            }, {
            date: '2016-05-07',
            name: '王小虎',
            address: '上海市普陀区金沙江路 1518 弄'
        }],
        validateRule:{
          name: [
            { required: false, message: '请输入角色', trigger: 'blur' },
            { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
          ]
        },
        multipleSelection: [],
        dialogTitle:'添加角色',
        dialogFormVisible:false,
        page: 1,
        rows: 10,
        totalItem:0,
        currentDialogType:''
      }
  },
  created(){
      this.hanleGetListAll();
  },
  methods:{
      search() {
        this.$store.dispatch('GetRoleList',{name:this.form.roleName}).then(res=>{
          if(res.result === 'success'){
            let _data = res.data;
            this.totalItem = _data.total;
            this.roleList = _data.rows;
            // 暂时没有数据
          }
        }).catch(err=>{
          console.log(err)
          this.$message({
            type: 'warning',
            message: '没有相关结果'
          }); 
        })
      },
      handleSelectionChange() {

      },
      addCount() {
          this.dialogFormVisible = true;
          this.dialogTitle = '添加角色';
          this.currentDialogType = 'add';
      },
      editRole(index, data) {
          this.dialogFormVisible = true;
          this.dialogTitle = '编辑角色';
          this.currentDialogType = 'edit';
          this.$store.dispatch('GetSingleRole', {id: data.id}).then(res=>{
            let data = res.data;
            if(res.result === 'success'){
              this.dialogForm.roleName = data.name;
              // this.dialogForm.userPwd = data.password;
            }
          })
      },
      delRole(index ,data) {
        this.$confirm('确认删除该角色？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$store.dispatch('DeleteRole',{id: data.id}).then(res=>{
            this.hanleGetListAll();
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
          }).catch(err=>{
            console.log(err);
          })
          
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
      },
      handleDialogSubmit(editrole){
        console.log(this.$refs)
        // this.$refs.editrole.validate((valid)=>{
          // if(valid){
            if(this.currentDialogType === 'add'){
              this.$store.dispatch('AddRole',{
                name: this.dialogForm.roleName,
                resourcesId: this.dialogForm.rolePermission
              }).then(res=>{
                this.hanleGetListAll();
                this.clearDialogForm();
              }).catch(err=>{
                console.log(err)
              })
            }else {
              this.$store.dispatch('UpdateRole',{
                name: this.dialogForm.roleName,
                resourcesId: this.dialogForm.rolePermission
              }).then(res=>{
                this.hanleGetListAll();
                this.clearDialogForm();
              }).catch(err=>{
                console.log(err)
              })
            }
            
          // }
        // })
      },
      hanleGetListAll(){
        this.$store.dispatch('GetRoleListALl',{page: this.page,rows: this.rows}).then(res => {
          console.log(res)
          if(res.result === 'success'){
            this.totalItem = res.data.length;
            this.roleList = res.data;
          }
        });
      },
      handleCancelSubmit(){
        this.clearDialogForm();
      },
      clearDialogForm(){
        this.dialogFormVisible = false;
        this.dialogForm.roleName = '';    
        this.dialogForm.rolePermission = '';
        this.currentDialogType = '';
      }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.user {
  &-container {
    margin: 30px;
  }
  &-text {
    font-size: 30px;
    line-height: 46px;
  }
  &-pagination {
      margin-top:50px;
  }
  &-table {
      margin-top:20px;
  }
}
</style>